package com.snail.readyvr.rest;

import com.snail.readyvr.mvp.model.BaseModel;
import com.snail.readyvr.util.ApiException;

import rx.functions.Func1;

/**
 * Created by MRKING on 2016/10/10.
 * 用来统一处理Http的ec,并将BaseModel的res部分剥离出来返回给subscriber
 *
 * @param <T> Subscriber真正需要的数据类型，也就是res部分的数据类型
 */
public class HttpResultFunc<T> implements Func1<BaseModel<T>, T> {

    private int whichApi;

    public HttpResultFunc(int whichApi) {
        this.whichApi = whichApi;
    }

    @Override
    public T call(BaseModel<T> httpResult) {
        if (httpResult.getCode() != 0) {
            throw new ApiException(httpResult.getCode(),httpResult.getMessage(),whichApi);
        }
        return httpResult.getData();
    }
}
